package com.lzb.browser.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.CleanUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.browser.BaseAc;
import com.lzb.browser.Content;
import com.lzb.browser.R;
import com.lzb.browser.basic.utils.SPUtil;
import com.lzb.browser.databinding.ActivityClearBrowseDataBinding;
import com.lzb.browser.utils.AppSpUtils;

import java.util.ArrayList;

public class ClearBrowseDataActivity extends BaseAc<ActivityClearBrowseDataBinding> implements View.OnClickListener {

    private boolean isClearCache = true, isClearRecord, isClearCookie, isRemind;

    @Override
    protected int onCreate() {
        return R.layout.activity_clear_browse_data;
    }

    @Override
    protected void initView() {
        mDataBinding.ivClearBrowseDataBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llCacheFile.setOnClickListener(this);
        mDataBinding.llRecord.setOnClickListener(this);
        mDataBinding.llCookie.setOnClickListener(this);
        mDataBinding.llClearData.setOnClickListener(this);
        mDataBinding.ivRemind.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llCacheFile) {//缓存文件和输入记录
            isClearCache = !isClearCache;
            mDataBinding.ivCacheFile.setImageResource(isClearCache ? R.drawable.select : R.drawable.no_select);
        } else if (id == R.id.llRecord) {//历史记录
            isClearRecord = !isClearRecord;
            mDataBinding.ivRecord.setImageResource(isClearRecord ? R.drawable.select : R.drawable.no_select);
        } else if (id == R.id.llCookie) {//cookie
            isClearCookie = !isClearCookie;
            mDataBinding.ivCookie.setImageResource(isClearCookie ? R.drawable.select : R.drawable.no_select);
        } else if (id == R.id.llClearData) {//清除数据
            if (!isClearCache && !isClearRecord && !isClearCookie) return;
            if (isClearCache) {
                CleanUtils.cleanInternalCache();
            }
            if (isClearRecord) {
                AppSpUtils.saveRecordList(new ArrayList<>());
            }
            if (isClearCookie) {
                CookieManager cookieManager = CookieManager.getInstance();
                cookieManager.removeAllCookies(null);
                cookieManager.flush();
            }
            ToastUtils.showShort(getString(R.string.clear_suc));
        } else if (id == R.id.ivRemind) {//退出时提醒清除历史记录
            isRemind = !isRemind;
            mDataBinding.ivRemind.setImageResource(isRemind ? R.drawable.akai : R.drawable.aguan);
            SPUtil.putBoolean(mContext, Content.isRemindClearRecord, isRemind);
        }
    }

    @Override
    protected void initData() {
        isRemind = SPUtil.getBoolean(mContext, Content.isRemindClearRecord, false);
        mDataBinding.ivRemind.setImageResource(isRemind ? R.drawable.akai : R.drawable.aguan);
    }


}